package com.colin.aiyi.ui.Home.yulu;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.colin.aiyi.R;
import com.colin.aiyi.adapter.SentenceAdapter;
import com.colin.aiyi.app.Constant;
import com.colin.aiyi.entity.BaseRequest;
import com.colin.aiyi.entity.User;
import com.colin.aiyi.entity.YiJuAndYiShuoEntity;
import com.colin.aiyi.fragment.BaseFragment;
import com.colin.aiyi.view.CustomLoadMoreView;
import com.colin.aiyi.view.ShowMsgUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import in.srain.cube.views.ptr.PtrClassicFrameLayout;
import in.srain.cube.views.ptr.PtrDefaultHandler;
import in.srain.cube.views.ptr.PtrFrameLayout;

/********
 * 语录
 */

public class SentenceFragment2WithNewRefresh extends BaseFragment implements IViewYuLu {

    @BindView(R.id.recycler_view)
    RecyclerView mRecyclerView;
    Unbinder unbinder;

    @BindView(R.id.text_no_data)
    TextView textNoData;
    @BindView(R.id.fragment_rotate_header_with_text_view_frame)
    PtrClassicFrameLayout hFrame;
    private List<YiJuAndYiShuoEntity> myWordList = new ArrayList<>();
    private SentenceAdapter headerAndFooterAdapter;
    private CustomLoadMoreView customLoadMoreView;
    BaseRequest request = new BaseRequest();
    private User user;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.layout_only_recyclerview2with_new_refresh, container, false);
        unbinder = ButterKnife.bind(this, view);
        iPresenter = new ImpPYuLu(this);
        initView(inflater);
        return view;
    }

    void initView(LayoutInflater inflater) {
        hFrame.setLastUpdateTimeRelateObject(this);
        hFrame.setPtrHandler(new PtrDefaultHandler() {
            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                iPresenter.onRefresh();
            }
        });
        headerAndFooterAdapter = new SentenceAdapter(myWordList);
        headerAndFooterAdapter.setOnItemChildClickListener(iPresenter);
        headerAndFooterAdapter.openLoadAnimation();
        headerAndFooterAdapter.setOnLoadMoreListener(iPresenter, mRecyclerView);
        customLoadMoreView = new CustomLoadMoreView();
        headerAndFooterAdapter.setLoadMoreView(customLoadMoreView);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        mRecyclerView.setAdapter(headerAndFooterAdapter);
    }

    @Override
    public void onResume() {
        super.onResume();
        request.setOrderType(Constant.TAB_TYPE_LIST);
        iPresenter.discourseList(request);

    }

    @Override
    protected void lazyLoad() {

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    public FragmentManager getFM() {
        return getChildFragmentManager();
    }

    @Override
    public List<YiJuAndYiShuoEntity> getData() {
        return myWordList;
    }

    @Override
    public void startNextAc(Intent intent) {
        startActivity(intent);
    }

    @Override
    public void pullMoreComlite(String... msg) {
        headerAndFooterAdapter.loadMoreComplete();
        ShowMsgUtil.hideLoadingDialog();
    }

    @Override
    public void pullMoreNoMore(String... msg) {
        headerAndFooterAdapter.loadMoreComplete();
        customLoadMoreView.setLoadMoreStatus(CustomLoadMoreView.STATUS_END);
        ShowMsgUtil.hideLoadingDialog();
    }

    @Override
    public void refreshComplite(String... msg) {
        hFrame.refreshComplete();
        headerAndFooterAdapter.notifyDataSetChanged();
        customLoadMoreView.setLoadMoreStatus(CustomLoadMoreView.STATUS_DEFAULT);
        textNoData.setVisibility(View.GONE);
        ShowMsgUtil.hideLoadingDialog();
    }

    @Override
    public void refreshNoData(String... msg) {
        textNoData.setVisibility(View.VISIBLE);
        ShowMsgUtil.hideLoadingDialog();
    }
}
